package com.ruoyi.controller;

import lombok.Data;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.xy.DefaultXYDataset;

import java.awt.*;
import java.io.File;
import java.io.IOException;

@Data
public class BmcChart {

    public String savePath = null;
    public String fileName = "chart";
    public String filePath;

    public void setFileName(String fileName) {
        this.fileName = fileName+ ".png";
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }


    public void init() {
        //创建主题样式
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("微软雅黑", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("微软雅黑", Font.PLAIN, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("微软雅黑", Font.PLAIN, 15));

        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);

    }


    public boolean save() {


        // 创建数据集
        DefaultXYDataset dataset = new DefaultXYDataset();

        // 生成第一条曲线数据 女性
        double[][] data1 = {{15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75}, {0.340, 0.381, 0.412, 0.409, 0.396, 0.376, 0.354, 0.334, 0.322, 0.312, 0.304, 0.298, 0.294}};
        dataset.addSeries("女", data1);

        // 生成第二条曲线数据
        double[][] data2 = {{15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75}, {0.386, 0.427, 0.458, 0.455, 0.442, 0.422, 0.400, 0.380, 0.368, 0.358, 0.350, 0.344, 0.340}};
        dataset.addSeries("男", data2);

        double[][] data3 = {{28,29}, {0.5,0.5}};
        dataset.addSeries("我", data3);

        // 创建图表
        JFreeChart chart = ChartFactory.createXYLineChart("骨密度指数表", "年龄(岁)", "骨密度cm²", dataset);

        chart.getTitle().setPaint(Color.BLUE);//设置标题颜色

//        chart.setBorderVisible(false);
//        chart.setBackgroundPaint(null);
//        chart.setBackgroundImageAlpha(0.0f);

//        ChartPanel chartPanel = new ChartPanel(chart);
//        chartPanel.setBackground(new Color(250,0,0,50));



        XYPlot plot = chart.getXYPlot();
        plot.setBackgroundAlpha(0.2F);//背景透明



        // 设置网格横线颜色
        plot.setRangeGridlinePaint(Color.gray);
        plot.setBackgroundAlpha(0.0F);
        // 设置网格横线大小
        plot.setRangeGridlineStroke(new BasicStroke(0.2F));


        ValueAxis axisX = plot.getRangeAxis();//获取柱状
//        axisX.setAutoRange(false);

//        axisX.setRange(new Range(0.5F,2.0F));
//        axisX.setLowerBound(0.5F);
//        axisX.setUpperBound(2.0F);



        ValueAxis axisY = plot.getDomainAxis(); //获取X轴，注意这里使用的是domainAxis，而不是rangeAxis
//        axisY.setLowerBound(15); //设置X轴最小值为1
//        axisY.setUpperBound(75); //设置X轴最大值为75，根据需要你可以调整这个值
//
//        plot.setDataset(dataset);

        // 保存图表为JPEG文件
        try {
            if (savePath != null) {
                this.filePath = savePath + "/" + fileName ;
            }else{
                this.filePath = fileName ;
            }
            ChartUtils.saveChartAsJPEG(new File(this.filePath), chart, 500, 400);


        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
